#include "UserModel.h"

list<USER_T> UserModel::query_all_user()
{
    list<USER_T> user_list;
    ConnectionPool*pool=ConnectionPool::getConnectionPool();
	shared_ptr<Connect>con=pool->getConnection();
    string sql="select * from users";
    PreparedStatement*pstmt=con->getConnection()->prepareStatement(sql);
    ResultSet*rs=pstmt->executeQuery();
    cout<<"查询所有用户信息"<<endl;

    while(rs->next())
    {
        USER_T user={0};
        user.user_id=atoi(rs->getString("username").c_str());
        strcpy(user.user_name,rs->getString("nickname").c_str());
        strcpy(user.user_pwd,rs->getString("password").c_str());
        strcpy(user.user_sex,rs->getString("gender").c_str());
        cout<<"username:"<<user.user_id<<" name:"<<user.user_name<<" pwd:"<<user.user_pwd<<" sex:"<<user.user_sex<<endl;
        user_list.push_back(user);
    }
    //释放资源
    delete rs;
    delete pstmt;
    return user_list;  
    
}

bool UserModel::query_user(int id, string pwd)
{
    ConnectionPool*pool=ConnectionPool::getConnectionPool();
	shared_ptr<Connect>con=pool->getConnection();
    string sql="select * from users where id=? and password=?;";
    PreparedStatement*pstmt=con->getConnection()->prepareStatement(sql);
    pstmt->setInt(1,id);
    pstmt->setString(2,pwd);
    ResultSet*rs=pstmt->executeQuery();
    if(rs->next())
    {
        return true;
    }
    return false;
}

vector<FRI_T> UserModel::query_all_Friend(int id)
{
    vector<FRI_T> fri_list;
    fri_list.clear();

    ConnectionPool*pool=ConnectionPool::getConnectionPool();
    shared_ptr<Connect>con=pool->getConnection();
    string sql="SELECT users.username,users.nickname, users.avatar FROM users JOIN friends ON users.id = friends.fri_account WHERE friends.my_account = ?;";
    PreparedStatement*pstmt=con->getConnection()->prepareStatement(sql);
    pstmt->setInt(1,id);
    ResultSet*rs=pstmt->executeQuery();
    while(rs->next())
    {

        FRI_T fri={0};
        cout<<"好友的路径大小"<<sizeof(fri.friend_path)<<endl;
        fri.friend_id=atoi(rs->getString("username").c_str());
        cout<<"好友id:"<<fri.friend_id<<endl;
        strcpy(fri.friend_name,rs->getString("nickname").c_str());
        cout<<"好友name:"<<fri.friend_name<<endl;
        strcpy(fri.friend_path,rs->getString("avatar").c_str());
        cout<<"路径长度:"<<rs->getString("avatar").length()<<endl;
        cout<<"好友path:"<<fri.friend_path<<endl;
        fri_list.push_back(fri);
        cout<<"查询的好友数量"<<fri_list.size()<<endl;
    }
   
    delete rs;
    delete pstmt;
    return fri_list;  
}

vector<CHAT_T> UserModel::query_all_chat(int send_id,int recv_id)
{
    cout<<"send_id "<<send_id<<" recv_id "<<recv_id<<endl;
    vector<CHAT_T> chat_list;
    ConnectionPool*pool=ConnectionPool::getConnectionPool();
    shared_ptr<Connect>con=pool->getConnection();
    string sql="select * from chatrecords where (sender_id = ? and receiver_id =?) or (sender_id =? and receiver_id =?);";
    PreparedStatement*pstmt=con->getConnection()->prepareStatement(sql);
    pstmt->setInt(1,send_id);
    pstmt->setInt(2,recv_id);
    pstmt->setInt(3,recv_id);
    pstmt->setInt(4,send_id);
    cout<<"查询聊天记录"<<endl;
    ResultSet*rs=pstmt->executeQuery();

    while(rs->next())
    {
        string  msg;
        CHAT_T chat={0};
        chat.send_id=atoi(rs->getString("sender_id").c_str());
        chat.recv_id=atoi(rs->getString("receiver_id").c_str());
        if(chat.send_id==send_id) msg="我说:"+rs->getString("message")+"\n";
        else
        {
            cout<<"聊天对象id:"<<chat.send_id<<endl;
            msg=to_string(chat.send_id)+"说:"+rs->getString("message")+"\n";
        } 
        cout<<"聊天记录:"<<msg<<endl;
        strcpy(chat.masg,msg.c_str());
        chat_list.push_back(chat);
    }
    delete rs;
    delete pstmt;
    return chat_list;
}

bool UserModel::insert_chat(int send_id, int recv_id, string msg)
{
    ConnectionPool*pool=ConnectionPool::getConnectionPool();
    shared_ptr<Connect>con=pool->getConnection();

      // 获取当前时间戳
    std::time_t now = std::time(nullptr);
    
    // 转换为本地时间结构
    std::tm* localTime = std::localtime(&now);
    
    // 输出年、月、日
    std::cout << "当前日期: " 
              << localTime->tm_year + 1900 << "-"  // 年份从1900开始
              << localTime->tm_mon + 1 << "-"     // 月份从0开始
              << localTime->tm_mday+1 << std::endl;
    string time=to_string(localTime->tm_year + 1900)+"-"+to_string(localTime->tm_mon + 1)+"-"+to_string(localTime->tm_mday+1);
    string sql="insert into chatrecords(sender_id,receiver_id,message,create_time) values(?,?,?,?);";
    PreparedStatement*pstmt=con->getConnection()->prepareStatement(sql);
    pstmt->setInt(1,send_id);
    pstmt->setInt(2,recv_id);
    pstmt->setString(3,msg);
    pstmt->setString(4,time);
    if(pstmt->executeUpdate()==1)
    {
        cout<<"插入聊天记录成功"<<endl;
        return true;
    }
    return false;
}
